package threadBaseUse;

/**
 * @description: 线程的终止和退出测试
 * @author: Liuwanqing
 * @date: 2022-10-14 17:00
 */
public class ThreadExit_ {
    public static void main(String[] args) throws InterruptedException {
        Th t = new Th();
        t.start();

        // main 通过控制 loop ，让 t 终止 —— 通知方式
        Thread.sleep(5 * 1000);
        t.setLoop(false);
    }
}

class Th extends  Thread {

    private int count = 0;
    private boolean loop = true; // 控制线程终止

    public void setLoop(boolean loop) {
        this.loop = loop;
    }

    @Override
    public void run() {
        while (loop){
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("T 在运行了~~~");
        }
    }
}
